package com.easyfun.healthmagicbox.widget;

import java.util.Calendar;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TimePicker;

import com.easyfun.healthmagicbox.R;

public class DateTimeOnTouchListener implements OnTouchListener{
	Activity activity;
	EditText inputdatetime;
	public DateTimeOnTouchListener(Activity activity,EditText inputdatetime)
	{
		this.activity = activity;
		this.inputdatetime = inputdatetime;
	}
	@Override
	public boolean onTouch(View v, MotionEvent event) {
		if (event.getAction() == MotionEvent.ACTION_DOWN) {

			AlertDialog.Builder builder = new AlertDialog.Builder(
					activity);
			View view = View.inflate(activity,
					R.layout.dialogdatetime, null);
			final DatePicker datePicker = (DatePicker) view
					.findViewById(R.id.date_picker);
			final TimePicker timePicker = (android.widget.TimePicker) view
					.findViewById(R.id.time_picker);
			builder.setView(view);

			Calendar cal = Calendar.getInstance();
			cal.setTimeInMillis(System.currentTimeMillis());
			datePicker.init(cal.get(Calendar.YEAR),
					cal.get(Calendar.MONTH),
					cal.get(Calendar.DAY_OF_MONTH), null);

			timePicker.setIs24HourView(true);
			timePicker.setCurrentHour(cal.get(Calendar.HOUR_OF_DAY));
			timePicker.setCurrentMinute(Calendar.MINUTE);

			builder.setTitle("选取数据产生时间");
			builder.setPositiveButton("确  定",
					new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog,
								int which) {

							StringBuffer sb = new StringBuffer();
							sb.append(String.format("%d-%02d-%02d",
									datePicker.getYear(),
									datePicker.getMonth() + 1,
									datePicker.getDayOfMonth()));
							sb.append(" ");
							sb.append(String.format("%02d:%02d",
									timePicker.getCurrentHour(),
									timePicker
									.getCurrentMinute()));

							inputdatetime.setText(sb);
							inputdatetime.requestFocus();

							dialog.cancel();
						}
					});

			Dialog dialog = builder.create();
			dialog.show();
		}

		return true;
	}
}
